package skyinline.j2ee.basic.projectv7.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import skyinline.j2ee.basic.projectv5.model.BoardVO;
import skyinline.j2ee.basic.util.jdbc.MariaDB;

public class BoardDAO extends 
		skyinline.j2ee.basic.projectv5.dao.BoardDAO {

	protected static int perPage = 20;		// 페이지당 게시물 수
	
	private static String pagingSQL = 
		"select bdno, title, uid, wdate, reades "
		+ " from boardv4 order by bdno desc "
		+ " limit ?, ?";
	protected static String totalSQL = 
		"select count(bdno) from boardv4";
	
	public static List<BoardVO> listBoard( 
				int startno ) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<BoardVO> result = new ArrayList<>();
		
		try {
			conn = MariaDB.openConn();
			pstmt = conn.prepareStatement(pagingSQL);
				pstmt.setInt(1, startno);
				pstmt.setInt(2, perPage);
			rs = pstmt.executeQuery();
			
			while(rs.next()) {
				BoardVO b = new BoardVO();
					b.setBdno( rs.getString(1) );
					b.setTitle( rs.getString(2) );
					b.setUid( rs.getString(3) );
					b.setWdate( rs.getString(4) );
					b.setReades( rs.getString(5) );
				result.add(b);	
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			MariaDB.closeConn(conn, pstmt, rs);
		}
		
		return result;
	}
	
	public static int totalBoard() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int result = 0;
		
		try{
			conn = MariaDB.openConn();
			pstmt = conn.prepareStatement(totalSQL);
			rs = pstmt.executeQuery();
			if (rs.next()) result = rs.getInt(1);			
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			MariaDB.closeConn(conn, pstmt, rs);
		}

		return result;
	}
	
}






